function [C, gamma, max_cv_accuracy] = svm_grid_search(filename, cv_mode)
[data_label, data_inst] = libsvmread(filename);
max_cv_accuracy = 0;
C = 0;
gamma = 0;

gammas = [0.025, 0.05, 0.1, 0.2, 0.4, 0.8, 1.6, 3.2, 6.4, 12.8];
for pow2c = -5:1:10,
    for gamma_index = 1:1:10,
        cur_gamma = gammas(gamma_index);
        options = ['-t 2', ' -c ', num2str(2^pow2c), ' -g ', num2str(cur_gamma), ' -v ', int2str(cv_mode)];
        cv_accuracy = svmtrain(data_label, data_inst, strcat(options));
        if (cv_accuracy > max_cv_accuracy),
            max_cv_accuracy = cv_accuracy;
            C = 2^pow2c;
            gamma = cur_gamma;
        end
    end
end

fprintf('The best accuracy: %g, c=%g, gamma=%g.\n', max_cv_accuracy, C, gamma);
end